import uuid
from dataclasses import dataclass
from typing import Any

import pytest
from sqlalchemy import insert
from sqlalchemy.ext.asyncio import AsyncSession

from recruit.adapter.database.gateway import ResumeInfoGateway
from recruit.adapter.database.models import Resume, ResumeInfo
from .fixtures.resume import ResumeTest
from .utils import check_attr_model, delete_user, check_get_one, get_resume


@dataclass
class ResumeInfoTest:
    id: uuid.uuid4()
    resume_id: uuid.uuid4()
    name: str
    patronymic: str
    surname: str
    residence: str


async def _check_resume(
        session: AsyncSession,
        data: ResumeInfoTest
) -> None:
    resume_info = await get_resume(session, ResumeInfo, data.id)
    assert isinstance(resume_info, ResumeInfo)
    check_get_one(
        resume_info,
        data,
        ('id', 'resume_id', 'name', 'patronymic', 'surname', 'residence')
    )


async def _create_resume_info(
        session: AsyncSession,
        data: dict[str, Any]
) -> ResumeInfoTest:
    stmt = insert(ResumeInfo).values(**data).returning(ResumeInfo)
    resume_info = (await session.execute(stmt)).scalar_one()
    resume_info = ResumeInfoTest(
        id=resume_info.id,
        resume_id=resume_info.resume_id,
        name=resume_info.name,
        patronymic=resume_info.patronymic,
        surname=resume_info.surname,
        residence=resume_info.residence
    )
    await session.commit()
    return resume_info


async def test_create(
        db_session: AsyncSession,
        get_resume_info: dict[str, Any],
        create_resume: ResumeTest
) -> None:
    resume_info_gateway = ResumeInfoGateway(db_session)
    get_resume_info['resume_id'] = create_resume.id
    resume_info = await resume_info_gateway.create(
        **get_resume_info
    )
    assert isinstance(resume_info.resume, Resume)
    check_attr_model(resume_info, get_resume_info)
    await db_session.commit()
    await delete_user(db_session, create_resume.user_id)


@pytest.mark.parametrize(
    'keys', [
        ('id', 'resume_id', 'name', 'patronymic', 'surname', 'residence')
    ]
)
async def test_get_one(
        keys: tuple[str, ...],
        db_session: AsyncSession,
        get_resume_info: dict[str, Any],
        create_resume: ResumeTest
) -> None:
    resume_info_gateway = ResumeInfoGateway(db_session)
    get_resume_info['resume_id'] = create_resume.id
    created_resume_info = await _create_resume_info(
        db_session,
        get_resume_info
    )
    for key, value in get_resume_info.items():
        resume_info = await resume_info_gateway.get_one(**{key: value})
        assert resume_info is not None
        check_get_one(resume_info, created_resume_info, keys)
    await delete_user(db_session, create_resume.user_id)


async def test_check_resume(
        db_session: AsyncSession,
        get_resume_info: dict[str, Any],
        create_resume: ResumeTest
) -> None:
    resume_info_gateway = ResumeInfoGateway(db_session)
    get_resume_info['resume_id'] = create_resume.id
    created_resume_info = await _create_resume_info(
        db_session,
        get_resume_info
    )
    val = await resume_info_gateway.check_resume(
        created_resume_info.resume_id
    )
    assert val
    await delete_user(db_session, create_resume.user_id)
    val = await resume_info_gateway.check_resume(
        created_resume_info.resume_id
    )
    assert not val


async def test_delete(
        db_session: AsyncSession,
        get_resume_info: dict[str, Any],
        create_resume: ResumeTest
) -> None:
    resume_info_gateway = ResumeInfoGateway(db_session)
    get_resume_info['resume_id'] = create_resume.id
    created_resume_info = await _create_resume_info(
        db_session,
        get_resume_info
    )
    await resume_info_gateway.delete(created_resume_info.id)
    await db_session.commit()
    await delete_user(db_session, create_resume.user_id)
    resume_info = await get_resume(
        db_session,
        ResumeInfo,
        created_resume_info.id
    )
    assert resume_info is None
